package org.easy.parse.html;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Node;
import org.jsoup.select.Elements;

/**
 * 
 * @Title: HtmlTextParser.java
 * @Package org.easy.parse.html
 * @Description: HTML 文本解析器
 * @author linNaibin
 * @date 2020年7月7日 上午10:47:14
 * @version V1.0
 */
public class HtmlTextParser {

	static String html = "<img src=\"ieg_tulip.jpg\" alt=\"上海鲜花港 - 郁金香\" >xxxx </img> <a href=\"xxxx.jpg\">aaaaa </a>";

	// static String html = "<div >xxxx </div>";

	private static Document parse(String html) {
		// 解析HTML字符串返回一个Document实现
		return Jsoup.parse(html);
	}

	public static Map<String, String> getElementsByTag(String html, String tagName, String... attrs) {

		Map<String, String> result = new HashMap<String, String>();

		Document document = parse(html);
		Elements elements = document.getElementsByTag(tagName);
		elements.forEach(e -> {

			for (int i = 0; i < attrs.length; i++) {
				 e.attr(attrs[i],"替换之后的地址.jpg");

				if (!e.attr(attrs[i]).isEmpty()) {
					result.put(attrs[i], e.attr(attrs[i]));
					
				}
				result.put("text", e.ownText());
			}

		});

		
		Elements elementsa = document.getElementsByTag("a");
		elementsa.get(0).attr("href", "tttttt.jrp");
		
		html =	document.html();
		result.forEach((String k, String v) -> {

			System.out.println("[key:" + k + "][value:" + v + "]");
		});
		// result
		// 替换一下 html
		
		
		
		
		System.out.println("替换之后:::" + html);
		
		return result;
	}

	public static void main(String[] args) {

	 	HtmlTextParser.getElementsByTag(html, "IMG", new String[] { "src" });
		
	//	HtmlTextParser.getElementsByTag(html, "a", new String[] { "href" });
		
		String f = "http://www.gxdxsyq.gov.cn/uploadfiles/pic/2018-5/cfab09ecb65c49d58404647a5e7a5298.png";
		
		System.out.println(f.replace("http://www.gxdxsyq.gov.cn", ""));

	}
}
